package org.agilelang.generator.javastruts2;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.agilelang.object.Controller;
import org.agilelang.object.Model;
import org.agilelang.object.Project;
import org.agilelang.object.attributes.Attribute;
import org.agilelang.object.attributes.Event;
import org.agilelang.object.attributes.MenuItem;
import org.agilelang.object.attributes.Reference;

public class GControllerTransaction extends GControllerSimple {

    Map menuMap = new HashMap();
    String menu = "";
    String menu2 = "";
    String submenu = "";
    Model model_header, model_detail;

    GControllerTransaction(String path_app, Project project, Controller controller) {
        super(path_app, project, controller);
        this.menus = project.menus;
        this.models = project.models;
        this.template2="javastruts2";


    }

    public void process() {
        String model_header_name = controller.model_header;
        String model_detail_name = controller.model_detail;
        List<Model> models = this.models;
        int t = models.size();
        for (int x = 0; x < t; x++) {
            Model model = models.get(x);
            if (model.name.equals(model_header_name)) {
                model_header = model;
            }
            if (model.name.equals(model_detail_name)) {
                model_detail = model;
            }
        }
        this.createDirectory(path + File.separator + "/web/view/" + controller.name.toLowerCase());
        menu2 = search_menu2(controller.name, "crud");
        menu = searchMenu(controller.name, "crud");
        submenu = searchMenuItem(controller.name, "crud");
        controller_py();
        controller_index();
        controller_create();
        controller_read();
    }

    private void controller_py() {




        String attributes_model_header = "",
                ref_header = "",
                import_header = "",
                param_header = "",
                param_header_get_set = "",
                param_header_all = "",
                js_save_header = "",
                py_save_ref_header = "",
                py_save_header = "";
        List<Attribute> attributes_header = model_header.attributes;
        int t = attributes_header.size();
        for (int x = 0; x < t; x++) {
            Attribute attr = attributes_header.get(x);
            js_save_header = js_save_header + "" + attr.name + " : $(\"#" + attr.name + "\").val(), ";
            if (attr.name.endsWith("_id")) {
                List<Reference> reference_header = model_header.references;
                int r = reference_header.size();
                for (int y = 0; y < r; y++) {
                    Reference ref = reference_header.get(y);
                    if (ref.foreign_key.equals(attr.name)) {
                        ref_header = ref_header + "        list" + ref.model_reference.toLowerCase() + " = " + ref.model_reference + ".all()\r\n";


                        import_header += "import model.entities." + ref.model_reference + ";\r\n";
                        import_header += "import model." + ref.model_reference + "Model;\r\n";
                        import_header += "import model.dao." + ref.model_reference + "DAO;\r\n";

                        param_header += "\t" + ref.model_reference + " " + ref.model_reference.toLowerCase() + " = new " + ref.model_reference + "();\r\n"
                                + "\tArrayList<" + ref.model_reference + "> " + this.setPluralize(ref.model_reference).toLowerCase() + " = new ArrayList();\r\n"
                                + "\t" + ref.model_reference + "DAO " + ref.model_reference.toLowerCase() + "Dao = new " + ref.model_reference + "Model();\r\n";

                        param_header_get_set += "\tpublic ArrayList<" + ref.model_reference + "> get" + this.setPluralize(ref.model_reference) + "() {\r\n"
                                + "\t\treturn this." + this.setPluralize(ref.model_reference).toLowerCase() + ";\r\n"
                                + "\t}\r\n"
                                + "\tpublic void set" + this.setPluralize(ref.model_reference) + "( ArrayList<" + ref.model_reference + "> " + this.setPluralize(ref.model_reference).toLowerCase() + ") {\r\n"
                                + "\t\tthis." + this.setPluralize(ref.model_reference).toLowerCase() + " = " + this.setPluralize(ref.model_reference).toLowerCase() + ";\r\n"
                                + "\t}\r\n";


                        param_header_all += "\t\t" + this.setPluralize(ref.model_reference).toLowerCase() + " = " + ref.model_reference.toLowerCase() + "Dao.all();\r\n";

                        py_save_ref_header = py_save_ref_header + ref.model_reference.toLowerCase() + " = db.get(db.Key.from_path('" + ref.model_reference + "', int(self.request.POST['" + attr.name + "'])))\n";

                        py_save_header = py_save_header + "\t\t" + model_header.name.toLowerCase() + ".set" + ref.model_reference + "((" + ref.model_reference + ") session.get(" + ref.model_reference + ".class,Integer.parseInt(getParameterValue(\"" + attr.name + "\"))));\n";
                        //purcharse.setClient((Client) session.get(Client.class, Integer.parseInt(getParameterValue("client_id"))));
                    }
                }

            } else {

                if (attr.type.equals("date")) {
                    py_save_header = py_save_header + "\t\t" + model_header.name.toLowerCase() + ".set" + name(attr.name) + "(new Date());\n";

                } else if (attr.type.equals("int") || attr.type.equals("integer")) {
                    if (!attr.name.equals("id")) {
                        py_save_header = py_save_header + "\t\t" + model_header.name.toLowerCase() + ".set" + name(attr.name) + "(Integer.parseInt(getParameterValue(\"" + attr.name + "\")));\n";
                    }


                } else if (attr.type.equals("float")) {
                    py_save_header = py_save_header + "\t\t" + model_header.name.toLowerCase() + ".set" + name(attr.name) + "(Float.parseFloat(getParameterValue(\"" + attr.name + "\")));\n";

                } else if (attr.type.equals("double")) {
                    py_save_header = py_save_header + "\t\t" + model_header.name.toLowerCase() + ".set" + name(attr.name) + "(Double.parseDouble(getParameterValue(\"" + attr.name + "\")));\n";

                } else {

                    py_save_header = py_save_header + "\t\t" + model_header.name.toLowerCase() + ".set" + name(attr.name) + "(getParameterValue(\"" + attr.name + "\"));\n";

                }
            }
        }

        String table_th_detail = "", attributes_model_detail = "", js_detail = "", js_td_detail = "", js_save_detail = "", py_save_ref_detail = "", py_save_detail = "";
        List<Attribute> attributes_detail = model_detail.attributes;
        t = attributes_detail.size();
        for (int x = 0; x < t; x++) {
            Attribute attr = attributes_detail.get(x);
            if (attr.name.equals(controller.relation_detail)) {
                continue;
            }
            table_th_detail = table_th_detail + "\t\t\t\t\t\t<th>" + attr.caption + "</th>\n";
            js_detail = js_detail + "var " + attr.name + " = $(\"#" + attr.name + "\");\n";
            js_td_detail = js_td_detail + "\"<td class=\\\"" + attr.name + "\\\">\" + " + attr.name + ".val() + \"</td>\" +\n";
            js_save_detail = js_save_detail + attr.name + ": $(this).children(\"." + attr.name + "\").html(), ";
            if (attr.name.endsWith("_id")) {
                List<Reference> reference_header = model_detail.references;
                int r = reference_header.size();
                for (int y = 0; y < r; y++) {
                    Reference ref = reference_header.get(y);
                    if (ref.foreign_key.equals(attr.name)) {
                        param_header += "\t" + ref.model_reference + " " + ref.model_reference.toLowerCase() + " = new " + ref.model_reference + "();\r\n"
                                + "\tArrayList<" + ref.model_reference + "> " + this.setPluralize(ref.model_reference).toLowerCase() + " = new ArrayList();\r\n"
                                + "\t" + ref.model_reference + "DAO " + ref.model_reference.toLowerCase() + "Dao = new " + ref.model_reference + "Model();\r\n";


                        import_header = import_header + "import model.entities." + ref.model_reference + ";\r\n";
                        import_header += "import model." + ref.model_reference + "Model;\r\n";
                        import_header += "import model.dao." + ref.model_reference + "DAO;\r\n";
                        param_header_all += "\t\t" + this.setPluralize(ref.model_reference).toLowerCase() + " = " + ref.model_reference.toLowerCase() + "Dao.all();\r\n";
                        param_header_get_set += "\tpublic ArrayList<" + ref.model_reference + "> get" + this.setPluralize(ref.model_reference) + "() {\r\n"
                                + "\t\treturn this." + this.setPluralize(ref.model_reference).toLowerCase() + ";\r\n"
                                + "\t}\r\n"
                                + "\tpublic void set" + this.setPluralize(ref.model_reference) + "( ArrayList<" + ref.model_reference + "> " + this.setPluralize(ref.model_reference).toLowerCase() + ") {\r\n"
                                + "\t\tthis." + this.setPluralize(ref.model_reference).toLowerCase() + " = " + this.setPluralize(ref.model_reference).toLowerCase() + ";\r\n"
                                + "\t}\r\n";

                        py_save_ref_detail = py_save_ref_detail + "\t\t\t" + ref.model_reference.toLowerCase() + " = db.get(db.Key.from_path('" + ref.model_reference + "', int(self.request.POST['detail['+str(pos)+'][" + attr.name + "]'])))\n";
                        //py_save_detail = py_save_detail + "\t\t\t\t" + attr.name + " = " + ref.model_reference.toLowerCase() + ".key(),\n";
                        py_save_detail += "\t\t" + model_detail.name.toLowerCase() + ".set" + ref.model_reference + "((" + ref.model_reference + ") session.get(" + ref.model_reference + ".class,Integer.parseInt(getParameterValue(\"detail[\" + i + \"][" + attr.name + "]\"))));\n";
                        

                    }
                }

            } else {
                if (attr.type.equals("date")) {
                    py_save_detail += "\t\t" + model_detail.name.toLowerCase() + ".set" + name(attr.name) + "(new Date());\n";

                } else if (attr.type.equals("int") || attr.type.equals("integer")) {
                    //py_save_detail = py_save_detail + "\t\t\t\t" + attr.name + " = int(self.request.POST['detail['+str(pos)+'][" + attr.name + "]']),\n";
                    if (!attr.name.equals("id")) {
                    py_save_detail += "\t\t" + model_detail.name.toLowerCase() + ".set" + name(attr.name) + "(Integer.parseInt(getParameterValue(\"detail[\" + i + \"][" + attr.name + "]\")));;\n";
                    }

                } else if (attr.type.equals("float")) {
                    //py_save_detail = py_save_detail + "\t\t\t\t" + attr.name + " = float(self.request.POST['detail['+str(pos)+'][" + attr.name + "]']),\n";
                    py_save_detail += "\t\t" + model_detail.name.toLowerCase() + ".set" + name(attr.name) + "(Float.parseFloat(getParameterValue(\"detail[\" + i + \"][" + attr.name + "]\")));;\n";

                } else if (attr.type.equals("double")) {
                    //py_save_detail = py_save_detail + "\t\t\t\t" + attr.name + " = float(self.request.POST['detail['+str(pos)+'][" + attr.name + "]']),\n";
                    py_save_detail += "\t\t" + model_detail.name.toLowerCase() + ".set" + name(attr.name) + "(Double.parseDouble(getParameterValue(\"detail[\" + i + \"][" + attr.name + "]\")));;\n";

                }
                
                else {
                    //py_save_detail = py_save_detail + "\t\t\t\t" + attr.name + " = self.request.POST['detail['+str(pos)+'][" + attr.name + "]'],\n";
                    py_save_detail += "\t\t" + model_detail.name.toLowerCase() + ".set" + name(attr.name) + "(getParameterValue(\"detail[\" + i + \"][" + attr.name + "]\"));;\n";

                }
            }
        }

        import_header += "import model.entities." + controller.model_header + ";\r\n";
        import_header += "import model." + controller.model_header + "Model;\r\n";
        import_header += "import model.dao." + controller.model_header + "DAO;\r\n";
        import_header += "import model.entities." + controller.model_detail + ";\r\n";
        import_header += "import model." + controller.model_detail + "Model;\r\n";
        import_header += "import model.dao." + controller.model_detail + "DAO;\r\n";

        param_header_get_set += "\tpublic ArrayList<" + controller.model_header + "> get" + this.setPluralize(controller.model_header) + "() {\r\n"
                + "\t\treturn this." + this.setPluralize(controller.model_header).toLowerCase() + ";\r\n"
                + "\t}\r\n"
                + "\tpublic void set" + this.setPluralize(controller.model_header) + "(ArrayList<" + controller.model_header + "> " + this.setPluralize(controller.model_header).toLowerCase() + ") {\r\n"
                + "\t\tthis." + this.setPluralize(controller.model_header).toLowerCase() + " = " + this.setPluralize(controller.model_header).toLowerCase() + ";\r\n"
                + "\t}\r\n"
                + "\tpublic " + controller.model_header + " get" + this.setSingularize(controller.model_header) + "() {\r\n"
                + "\t\treturn this." + this.setSingularize(controller.model_header).toLowerCase() + ";\r\n"
                + "\t}\r\n"
                + "\tpublic void set" + this.setSingularize(controller.model_header) + "(" + controller.model_header + " " + this.setSingularize(controller.model_header).toLowerCase() + ") {\r\n"
                + "\t\tthis." + this.setSingularize(controller.model_header).toLowerCase() + " = " + this.setSingularize(controller.model_header).toLowerCase() + ";\r\n"
                + "\t}\r\n";

        param_header_get_set += "\tpublic ArrayList<" + controller.model_detail + "> get" + this.setPluralize(controller.model_detail) + "() {\r\n"
                + "\t\treturn this." + this.setPluralize(controller.model_detail).toLowerCase() + ";\r\n"
                + "\t}\r\n"
                + "\tpublic void set" + this.setPluralize(controller.model_detail) + "(ArrayList<" + controller.model_detail + "> " + this.setPluralize(controller.model_detail).toLowerCase() + ") {\r\n"
                + "\t\tthis." + this.setPluralize(controller.model_detail).toLowerCase() + " = " + this.setPluralize(controller.model_detail).toLowerCase() + ";\r\n"
                + "\t}\r\n";


        param_header += "\t" + controller.model_detail + " " + controller.model_detail.toLowerCase() + " = new " + controller.model_detail + "();\r\n"
                + "\tArrayList<" + controller.model_detail + "> " + this.setPluralize(controller.model_detail).toLowerCase() + " = new ArrayList();\r\n"
                + "\t" + controller.model_detail + "DAO " + controller.model_detail.toLowerCase() + "Dao = new " + controller.model_detail + "Model();\r\n";

       
        String views_str = "";
        views_str += view(controller.views, controller.name);
        views_str += viewform(controller.forms, controller.name);
        views_str += viewreport(controller.reports, controller.name);

        String file = readStreamTpl("ct_controller.java.tpl");
        file = file.replace("//Header//", controller.model_header);
        file = file.replace("//header//", controller.model_header.toLowerCase());
        file = file.replace("//Detail//", controller.model_detail);
        file = file.replace("//detail//", controller.model_detail.toLowerCase());
        file = file.replace("//headers//", this.setPluralize(controller.model_header).toLowerCase());
        file = file.replace("//Controller//", controller.name);
        file = file.replace("//controller//", controller.name.toLowerCase());
        file = file.replace("//menu2//", menu2);
        file = file.replace("//menu//", menu);
        file = file.replace("//submenu//", submenu);
        file = file.replace("//views_str//", views_str);
        file = file.replace("//param_header_all//", param_header_all);
        file = file.replace("//param_header_get_set//", param_header_get_set);


        file = file.replace("//import_header//", import_header);
        file = file.replace("//ref_header//", ref_header);
        file = file.replace("//param_header//", param_header);
        file = file.replace("//NameClass//", controller.name);
        file = file.replace("//py_save_header//", py_save_header);
        file = file.replace("//py_save_detail//", py_save_detail);
        file = file.replace("//menu//", menu);
        createfile("src/java/controller/" + controller.name + "Controller.java", path, file);
    }

    private void controller_index() {
        String th = "", td = "";
        List<Attribute> attributes_header = model_header.attributes;
        int t = attributes_header.size();
        for (int x = 0; x < t; x++) {
            Attribute attr = attributes_header.get(x);
            if (attr.name.endsWith("_id")) {
                List<Reference> reference_header = model_header.references;
                int r = reference_header.size();
                for (int y = 0; y < r; y++) {
                    Reference ref = reference_header.get(y);
                    if (ref.foreign_key.equals(attr.name)) {
                        if (attr.name.equals("name")) {
                            td = td + "\t\t\t\t\t\t\t\t<td><a href=\"<s:url action=\"//link//read\" ><s:param name=\"id\" value=\"%{id}\"></s:param></s:url>\"><s:property value=\"name\" /></a></td>    \r\n";
                        } else {
                            td = td + "\t\t\t\t\t\t\t\t<td><s:property value=\"" + attr.name + "\" /></td> \r\n";
                        }
                        th = th + "<th>" + attr.caption + "</th>";
                    }
                }
            } else {
                if (attr.name.equals("name")) {
                    td = td + "\t\t\t\t\t\t\t\t<td><a href=\"<s:url action=\"//link//read\" ><s:param name=\"id\" value=\"%{id}\"></s:param></s:url>\"><s:property value=\"name\" /></a></td>    \r\n";
                } else {
                    td = td + "\t\t\t\t\t\t\t\t<td><s:property value=\"" + attr.name + "\" /></td> \r\n";
                }
                th = th + "<th>" + attr.caption + "</th>";
            }
        }

        String menu_1 = searchMenu(controller.name, "crud");
        String menu_2 = searchMenuItem(controller.name, "crud");

        String file = readStreamTpl("ct_index.jsp.tpl");
        file = file.replace("//th//", th);
        file = file.replace("//td//", td);
        file = file.replace("//menu//", menu);
        file = file.replace("//menu_1//", menu_1);
        file = file.replace("//menu_2//", menu_2);
        file = file.replace("//submenu//", submenu);
        file = file.replace("//headers//", this.setPluralize(model_header.name).toLowerCase());
        file = file.replace("//Controller//", controller.name);
        file = file.replace("//controller//", controller.name.toLowerCase());
        createfile("/web/view/" + controller.name.toLowerCase() + "/index.jsp", path, file);
    }

    private void controller_create() {


        String attributes_model_header = "", ref_header = "", import_header = "", param_header = "", js_save_header = "", py_save_ref_header = "", py_save_header = "";
        List<Attribute> attributes_header = model_header.attributes;
        int t = attributes_header.size();
        for (int x = 0; x < t; x++) {
            Attribute attr = attributes_header.get(x);

            if (attr.name.equals("id") || attr.name.equals("createdate")) {
                continue;
            }
            js_save_header = js_save_header + "\t\t\t" + attr.name + " : $(\"#" + attr.name + "\").val(),\n";
            if (attr.name.endsWith("_id")) {
                List<Reference> reference_header = model_header.references;
                int r = reference_header.size();
                for (int y = 0; y < r; y++) {
                    Reference ref = reference_header.get(y);
                    if (ref.foreign_key.equals(attr.name)) {
                        attributes_model_header +=
                                "\t<p>\n"
                                + "\t\t<s:select \n"
                                + "\t\t\tlabel=\"" + attr.caption + "\"\n"
                                + "\t\t\tlist=\"" + setPluralize(ref.model_reference).toLowerCase() + "\" \n"
                                + "\t\t\tname=\"" + attr.name + "\" \n"
                                + "\t\t\tlistValue=\"%{name}\"\n"
                                + "\t\t\tlistKey=\"%{id}\"\n"
                                + "\t\t\temptyOption=\"false\"\n"
                                + "\t\t\theaderKey=\"-1\"\n"
                                + "\t\t\theaderValue=\"- Selected -\"\n"
                                + "\t\t\tcssClass=\"text ui-widget-content ui-corner-all\"\n"
                                + "\t\t\tstyle=\"width: 215px;\"\n"
                                + "\t\t/>\n";
                    }
                }
            } else {
                if (attr.type.equals("date")) {
                    attributes_model_header = attributes_model_header
                            + "\t<p>\n"
                            + "\t\t<label for=\"" + attr.name + "\">" + attr.caption + " : </label>\n"
                            + "\t\t<input type=\"text\" name=\"" + attr.name + "\" id=\"" + attr.name + "\" style=\"width: 200px;\" class=\"datepicker text ui-widget-content ui-corner-all\"/>\n"
                            + "\t</p>\n";
                } else if (attr.type.equals("int") || attr.type.equals("integer")) {
                    attributes_model_header = attributes_model_header
                            + "\t<p>\n"
                            + "\t\t<label for=\"" + attr.name + "\">" + attr.caption + " : </label>\n"
                            + "\t\t<input type=\"text\" name=\"" + attr.name + "\" id=\"" + attr.name + "\" value=\"0\" style=\"width: 200px;\" class=\"text ui-widget-content ui-corner-all\"/>\n"
                            + "\t</p>\n";
                } else if (attr.type.equals("float")) {
                    attributes_model_header = attributes_model_header
                            + "\t<p>\n"
                            + "\t\t<label for=\"" + attr.name + "\">" + attr.caption + " : </label>\n"
                            + "\t\t<input type=\"text\" name=\"" + attr.name + "\" id=\"" + attr.name + "\" value=\"0\" style=\"width: 200px;\" class=\"text ui-widget-content ui-corner-all\"/>\n"
                            + "\t</p>\n";
                } else {
                    attributes_model_header = attributes_model_header
                            + "\t<p>\n"
                            + "\t\t<label for=\"" + attr.name + "\">" + attr.caption + " : </label>\n"
                            + "\t\t<input type=\"text\" name=\"" + attr.name + "\" id=\"" + attr.name + "\" style=\"width: 200px;\" class=\"text ui-widget-content ui-corner-all\"/>\n"
                            + "\t</p>\n";
                }
            }
        }

        String table_th_detail = "", attributes_model_detail = "", js_detail = "", js_td_detail = "", js_save_detail = "", py_save_ref_detail = "", py_save_detail = "";
        List<Attribute> attributes_detail = model_detail.attributes;
        t = attributes_detail.size();
        for (int x = 0; x < t; x++) {
            Attribute attr = attributes_detail.get(x);
            if (attr.name.equals("id") || attr.name.equals("createdate")) {
                continue;
            }

            if (attr.name.equals(controller.relation_detail)) {
                continue;
            }
            table_th_detail = table_th_detail + "\t\t\t\t\t\t<th>" + attr.caption + "</th>\n";
            js_detail = js_detail + "var " + attr.name + " = $(\"#" + attr.name + "\");\n";
            js_td_detail = js_td_detail + "\t\t\t\t\t\"<td class=\\\"" + attr.name + "\\\">\"+$(\"#" + attr.name + "\").val() + \"</td>\" +\n";
            js_save_detail = js_save_detail + "\t\t\t\t" + attr.name + ": $(this).children(\"." + attr.name + "\").html(),\n";
            if (attr.name.endsWith("_id")) {
                List<Reference> reference_header = model_detail.references;
                int r = reference_header.size();
                for (int y = 0; y < r; y++) {
                    Reference ref = reference_header.get(y);
                    if (ref.foreign_key.equals(attr.name)) {


                        py_save_ref_detail = py_save_ref_detail + "\t\t\t" + ref.model_reference.toLowerCase() + " = db.get(db.Key.from_path('" + ref.model_reference + "', int(self.request.POST['detail['+str(pos)+'][" + attr.name + "]'])))\n";
                        py_save_detail = py_save_detail + "\t\t\t\t" + attr.name + " = " + ref.model_reference.toLowerCase() + ".key(),\n";


                        attributes_model_detail +=
                                "\t<p>\n"
                                + "\t\t<s:select \n"
                                + "\t\t\tlabel=\"" + attr.caption + "\"\n"
                                + "\t\t\tlist=\"" + setPluralize(ref.model_reference).toLowerCase() + "\" \n"
                                + "\t\t\tname=\"" + attr.name + "\" \n"
                                + "\t\t\tlistValue=\"%{name}\"\n"
                                + "\t\t\tlistKey=\"%{id}\"\n"
                                + "\t\t\temptyOption=\"false\"\n"
                                + "\t\t\theaderKey=\"-1\"\n"
                                + "\t\t\theaderValue=\"- Selected -\"\n"
                                + "\t\t\tcssClass=\"text ui-widget-content ui-corner-all\"\n"
                                + "\t\t\tstyle=\"width: 215px;\"\n"
                                + "\t\t/>\n";

                    }
                }

            } else {
                if (attr.type.equals("date")) {

                    attributes_model_detail = attributes_model_detail
                            + "\t<p>\n"
                            + "\t\t<label for=\"" + attr.name + "\">" + attr.caption + " : </label>\n"
                            + "\t\t<input type=\"text\" name=\"" + attr.name + "\" id=\"" + attr.name + "\" style=\"width: 200px;\" class=\"datepicker text ui-widget-content ui-corner-all\"/>\n"
                            + "\t</p>\n";
                } else if (attr.type.equals("int") || attr.type.equals("integer")) {

                    attributes_model_detail = attributes_model_detail
                            + "\t<p>\n"
                            + "\t\t<label for=\"" + attr.name + "\">" + attr.caption + " : </label>\n"
                            + "\t\t<input type=\"text\" name=\"" + attr.name + "\" id=\"" + attr.name + "\" style=\"width: 200px;\" class=\"text ui-widget-content ui-corner-all\"/>\n"
                            + "\t</p>\n";
                } else if (attr.type.equals("float")) {

                    attributes_model_detail = attributes_model_detail
                            + "\t<p>\n"
                            + "\t\t<label for=\"" + attr.name + "\">" + attr.caption + " : </label>\n"
                            + "\t\t<input type=\"text\" name=\"" + attr.name + "\" id=\"" + attr.name + "\" style=\"width: 200px;\" class=\"text ui-widget-content ui-corner-all\"/>\n"
                            + "\t</p>\n";
                } else {

                    attributes_model_detail = attributes_model_detail
                            + "\t<p>\n"
                            + "\t\t<label for=\"" + attr.name + "\">" + attr.caption + " : </label>\n"
                            + "\t\t<input type=\"text\" name=\"" + attr.name + "\" id=\"" + attr.name + "\" style=\"width: 200px;\" class=\"text ui-widget-content ui-corner-all\"/>\n"
                            + "\t</p>\n";
                }
            }
        }

        this.createDirectory(path + File.separator + "view/" + controller.name.toLowerCase());
        String view_file = readStreamTpl("ct_create1.html.tpl");
        view_file = view_file.replace("//NameClass//", controller.name);
        
        view_file = view_file.replace("//controller//", controller.name.toLowerCase());
        
        view_file = view_file.replace("//attributes_model_header//", attributes_model_header);
        view_file = view_file.replace("//attributes_model_detail//", attributes_model_detail);
        view_file = view_file.replace("//table_th_detail//", table_th_detail);
        createfile("/web/view/" + controller.name.toLowerCase() + "/create.jsp", path, view_file);

        String selectDetail = "";
        String addDetail = "";
        String deleteDetail = "";
        String updateDetail = "";

        String event_insertDetail = "";
        List<Event> events = controller.events;
        int te = events.size();
        for (int x = 0; x < te; x++) {
            Event event = events.get(x);
            String action = event.action;


            action = action.replaceAll(" ", "") + " );";
            Pattern pattern = Pattern.compile("(header\\.)(.+?)(\\:)");
            Matcher matcher = pattern.matcher(action);
            List<String> listMatches = new ArrayList<String>();
            while (matcher.find()) {
                System.out.println(matcher.group(2));
                listMatches.add(matcher.group(2));
            }
            HashSet hs = new HashSet();
            hs.addAll(listMatches);
            listMatches.clear();
            listMatches.addAll(hs);
            for (int i = 0; i < listMatches.size(); i++) {
                System.out.println(listMatches.get(i).toString());
                String new_str = "\\$('#" + listMatches.get(i).toString() + "').val( ";
                action = action.replaceAll("header." + listMatches.get(i).toString() + ":", new_str);
            }

            System.out.println(action);
            //----------------------------------------------------------------------
            pattern = Pattern.compile("(header\\.)(.+?)(\\s|\\+|\\-|\\*|\\/)");
            matcher = pattern.matcher(action);
            listMatches = new ArrayList<String>();
            while (matcher.find()) {
                System.out.println(matcher.group(2));
                listMatches.add(matcher.group(2));
            }
            hs = new HashSet();
            hs.addAll(listMatches);
            listMatches.clear();
            listMatches.addAll(hs);
            for (int i = 0; i < listMatches.size(); i++) {
                System.out.println(listMatches.get(i).toString());
                String new_str = " parseFloat(\\$('#" + listMatches.get(i).toString() + "').val()) ";
                action = action.replaceAll("header." + listMatches.get(i).toString(), new_str);
            }

            System.out.println(action);
            //----------------------------------------------------------------------
            pattern = Pattern.compile("(detail\\.)(.+?)(\\s|\\+|\\-|\\*|\\/)");
            matcher = pattern.matcher(action);
            listMatches = new ArrayList<String>();
            while (matcher.find()) {
                System.out.println(matcher.group(2));
                listMatches.add(matcher.group(2));
            }
            hs = new HashSet();
            hs.addAll(listMatches);
            listMatches.clear();
            listMatches.addAll(hs);
            for (int i = 0; i < listMatches.size(); i++) {
                System.out.println(listMatches.get(i).toString());
                String new_str = " parseFloat(\\$('#" + listMatches.get(i).toString() + "').val()) ";
                action = action.replaceAll("detail." + listMatches.get(i).toString(), new_str);
            }
            action = action.replaceAll(":", "=");
            System.out.println(action);

            if (event.type.equals("selectDetail")) {
                selectDetail += action + "\n";
            }
            if (event.type.equals("addDetail")) {
                addDetail += action + "\n";
            }
            if (event.type.equals("deleteDetail")) {
                deleteDetail += action + "\n";
            }
            if (event.type.equals("plus_update")) {
                updateDetail += action + "\n";
            }
        }

        String menu_1 = searchMenu(controller.name, "crud");
        String menu_2 = searchMenuItem(controller.name, "crud");

        String js_file = readStreamTpl("ct_index1.js.tpl");
        js_file = js_file.replace("//js_detail//", js_detail);
        js_file = js_file.replace("//js_td_detail//", js_td_detail);
        js_file = js_file.replace("//js_save_detail//", js_save_detail);
        js_file = js_file.replace("//js_save_header//", js_save_header);
        js_file = js_file.replace("//event_insertDetail//", event_insertDetail);
        //js_file = js_file.replace("//selectDetail//", selectDetail);
        js_file = js_file.replace("//addDetail//", addDetail);
        js_file = js_file.replace("//deleteDetail//", deleteDetail);
        js_file = js_file.replace("//updateDetail//", updateDetail);
        js_file = js_file.replace("//menu_1//", menu_1);
        js_file = js_file.replace("//menu_2//", menu_2);
        js_file = js_file.replace("//controller//", controller.name.toLowerCase());

        createfile("web/js/_" + controller.name.toLowerCase() + ".js", path, js_file);

    }

    private void controller_read() {
        String th = "", td = "", header = "";
        List<Attribute> attributes_header = model_header.attributes;
        int t = attributes_header.size();
        for (int x = 0; x < t; x++) {
            Attribute attr = attributes_header.get(x);
            if (attr.name.endsWith("_id")) {
                List<Reference> reference_header = model_header.references;
                int r = reference_header.size();
                for (int y = 0; y < r; y++) {
                    Reference ref = reference_header.get(y);
                    if (ref.foreign_key.equals(attr.name)) {
                         header +="\t\t\t\t<s:textfield label=\"" + attr.name + "\" name=\"" + this.setSingularize(model_header.name).toLowerCase() + "." + attr.name + "\"   value=\"%{" + this.setSingularize(model_header.name).toLowerCase() + ".get" + name(attr.name) + "()}\" disabled=\"true\" /> \r\n";
                        /*header = header
                                + "<p>\n"
                                + "<label for=\"" + attr.name + "\">" + attr.caption + " : </label>\n"
                                + "<input type=\"text\" name=\"" + attr.name + "\" id=\"" + attr.name + "\" value=\" " + model_header.name.toLowerCase() + "." + attr.name + "." + ref.field_reference + " \"  class=\"text ui-widget-content ui-corner-all\" DISABLED/>\n"
                                + "</p>";*/
                    }
                }
            } else {
                header +="\t\t\t\t<s:textfield label=\"" + attr.name + "\" name=\"" + this.setSingularize(model_header.name).toLowerCase() + "." + attr.name + "\"   value=\"%{" + this.setSingularize(model_header.name).toLowerCase() + ".get" + name(attr.name) + "()}\" disabled=\"true\" /> \r\n";
                               ;;

            }
        }
        List<Attribute> attributes_detail = model_detail.attributes;
        t = attributes_detail.size();
        for (int x = 0; x < t; x++) {
            Attribute attr = attributes_detail.get(x);
            if (attr.name.endsWith("_id")) {
                List<Reference> reference_header = model_detail.references;
                int r = reference_header.size();
                for (int y = 0; y < r; y++) {
                    Reference ref = reference_header.get(y);
                    if (ref.foreign_key.equals(attr.name)) {
                        //td = td + "<td>{{" + model_detail.name.toLowerCase() + "." + attr.name + "." + ref.field_reference + "}}</td>";
                        td = td + "<td><s:property value=\"" + ref.model_reference + "." + ref.field_reference + "\"/></td>\n";
                        th = th + "<th>" + attr.caption + "</th>";
                    }
                }
            } else {
                td = td + "<td><s:property value=\"" + attr.name + "\"/></td>\n";
                th = th + "<th>" + attr.caption + "</th>";
            }
        }
        String file = readStreamTpl("ct_read1.html.tpl");
        file = file.replace("//th//", th);
        file = file.replace("//td//", td);
        file = file.replace("//menu//", menu);
        file = file.replace("//submenu//", submenu);
        file = file.replace("//header//", header);
        file = file.replace("//header_m//", controller.model_header.toLowerCase());
        file = file.replace("//ModelDetails//", this.setPluralize(controller.model_detail));
        file = file.replace("//controller_s//", this.setSingularize(controller.name).toLowerCase());
        file = file.replace("//detail//", model_detail.name.toLowerCase());
        createfile("/web/view/" + controller.name.toLowerCase() + "/read.jsp", path, file);
    }

    private String search_menu2(String controller, String view) {
        String menu = "";
        int t = menus.size();
        for (int i = 0; i < t; i++) {
            int tt = menus.get(i).menuitems.size();
            for (int ii = 0; ii < tt; ii++) {
                MenuItem menuitem = menus.get(i).menuitems.get(ii);
                if (menuitem.controller.equals(controller) && menuitem.action.equals(view)) {
                    menu = "'menu' : '" + menus.get(i).name.toLowerCase() + "','submenu' : '" + menuitem.name.toLowerCase() + "'";
                    break;
                }
            }
        }
        return menu;
    }

    private String search_submenu(String controller, String view) {
        String menu = "";
        int t = menus.size();
        for (int i = 0; i < t; i++) {
            int tt = menus.get(i).menuitems.size();
            for (int ii = 0; ii < tt; ii++) {
                MenuItem menuitem = menus.get(i).menuitems.get(ii);
                if (menuitem.controller.equals(controller) && menuitem.action.equals(view)) {
                    menu = menuitem.name.toLowerCase();
                    break;
                }
            }
        }
        return menu;
    }
}
